#ifndef FSXWSSETTINGS_H
#define FSXWSSETTINGS_H

#include <QObject>
#include <QSettings>
#include <QApplication>
#include <QDir>
#include "../FsxSimConnect/fsxsimconnect.h"
#include "../FsxWebService/requestdispatcher.h"
using namespace FsxWebService;
using namespace FsxSimConnect;

namespace FsxWsGUI {

//! Settings of the application, also used to pass the settings to the GUI.
class FsxWsSettings : public QObject
{
    Q_OBJECT

private:
    QString _settingsFilename;   /*!< path to settings file */
    int _fsxAircraftRadiusMeter; /*!< fetch aircrafts within this range */
    int _fsxModelMaxWaypoints;   /*!< max number of waypoints */
    bool _fsxModelMaxWaypointsDisabled;  /*!< use / disable max number of waypoints */
    int _fsxModelWaypointsTimeframeS;    /*!< waypoints need to be within timeframe */
    bool _fsxModelWaypointsTimeframeSDisabled; /*!< timeframe used / disabled */
    bool _fsxDumpStatistics; /*!< dump statistics about the callback types */
    bool _guiShowOnlyOwnAircraft; /*!< only show "my" aircraft */
    SIMCONNECT_PERIOD _fsxUpdatePeriod; /*! how often FSX updates [s] the aircrafts */
    QString _fsxIpAddress; /*! IP address of FSX machine */
    int     _fsxIpPort;    /*! port of FSX machine */
    int  _guiUpdateAircraftsMs; /*! how often the timer pulls the aircraft data for the list */
    bool _guiShowWaypointsWhenAircraftSelected; /*!< directly switch to waypoint view when aircraft is selected */
    int  _kmlLineStyleWidth;    /*!< Line style width (KML) */
    QString _kmlLineStyleColor; /*!< Line style color (KML) */
    QString _kmlPolyStyleColor; /*!< Poly style color (KML) */
    QString _kmlWaypointModel;  /*!< Name of the used KML model */
    QString _dirFmcData;      /*!< FMC data directory */
    bool _kmlWhenSavedUseLocalModel;  /*!< When a KML file is saved, a local file reference is used */
    bool _kmlWhenSavedUseFullPath;    /*!< When a KML file is saved, a local file + full path is used */
    int _kmlNetworkLinkUpdatePeriodS; /*!< Update frequency of reloading data with a network link (seconds) */
    bool _autoStartWebServer; /*!< automatically start the web server */
    bool _autoConnectFsx; /*!< automatically connect to FSX */
public:
    //! Standard Constructor.
    /*! \param parent Qt parent object */
    explicit FsxWsSettings(QObject *parent = 0);
    //! Considered radius
    /*! \return meters */
    int getFsxAircraftRadiusMeter() const { return _fsxAircraftRadiusMeter; }
    //! Considered radius
    /*! \return kilometer */
    double getFsxAircraftRadiusKilometer() const { return _fsxAircraftRadiusMeter / 1000; }
    //! Max. number of waypoints
    /*! \return number */
    int getFsxModelMaxWaypoints() const { return _fsxModelMaxWaypoints; }
    //! Considered timeframe
    /*! \return seconds */
    int getFsxModelWaypointsTimeframeS() const { return _fsxModelWaypointsTimeframeS; }
    //! IP address of machine where FSX is running, or 127.0.0.1 for localhost
    /*! \return QString */
    QString getFsxIpAddress() const { return _fsxIpAddress; }
    //! IP port for FSX connection
    /*! \return port*/
    int getFsxIpPort() const { return _fsxIpPort; }
    //! Time frame used?
    /*! \return disabled? */
    bool isFsxModelWaypointsTimeframeDisabled() const { return _fsxModelWaypointsTimeframeSDisabled; }
    //! Dump statistics info
    /*! \return dump */
    bool isFsxDumpStatisticsEnabled() { return _fsxDumpStatistics; }
    //! Max. waypoints used?
    /*! \return disabled? */
    bool isFsxModelMaxWaypointsDisabled() const { return _fsxModelMaxWaypointsDisabled; }
    //! Only display own aircraft?
    /*! \return own aircraft only */
    bool getGuiShowOnlyOwnAircraft() const { return _guiShowOnlyOwnAircraft; }
    //! Udpate period for aircrafts [ms]
    /*! \return milliseconds */
    int getGuiUpdateAircraftsMs() const { return _guiUpdateAircraftsMs; }
    //! Udpate period for aircrafts [s]
    /*! \return seconds */
    int getGuiUpdateAircraftsS() const { return _guiUpdateAircraftsMs / 1000; }
    //! Show waypoints when aircraft is selected?
    /*! \return waypoints */
    bool guiShowWaypointsWhenAircraftSelected() const { return _guiShowWaypointsWhenAircraftSelected; }
    //! Line style color
    /*! \return color */
    QString getKmlLineStyleColor() const { return _kmlLineStyleColor; }
    //! Poly style color
    /*! \return color */
    QString getKmlPolyStyleColor() const { return _kmlPolyStyleColor; }
    //! Name of the used KML model.
    /*! \return QString name */
    QString getKmlWaypointModel() const { return _kmlWaypointModel; }
    //! Full URL to the KML directory.
    /*! \return QString url */
    QString getKmlWaypointModelBaseUrl() const { return RequestDispatcher::getDocrootUrl() + "kml/"; }
    //! Full URL to the KML model.
    /*! \return QString url
        \sa getKmlWaypointModel() */
    QString getKmlWaypointModelUrl() const;
    /*! Navigraph directory
        \return directory */
    QString getFmcDataDirectory() const { return _dirFmcData; }
    //! Get model URL or file path depending on settings
    /*! \return QString url or file path */
    QString getKmlWaypointModelPathOrUrl() const;
    //! When a KML file is saved, a local file reference is used.
    /*! \return bool */
    bool getKmlWhenSavedUseLocalModel() const { return _kmlWhenSavedUseLocalModel; }
    //! When a KML file is saved, a local file + full path is used.
    /*! \return bool */
    bool getKmlWhenSavedUseFullPath() const { return _kmlWhenSavedUseFullPath; }
    //! KML line style width.
    /*! \return width */
    int getKmlLineStyleWidth() const { return _kmlLineStyleWidth; }
    //! KML network link update period [s].
    /*! \return period in seconds */
    int getKmlNetworkLinkUpdatePeriodS() const { return _kmlNetworkLinkUpdatePeriodS; }
    //! The update period of SimConnect
    /*! \return SIMCONNECT_PERIOD */
    SIMCONNECT_PERIOD getFsxUpdatePeriod() const { return _fsxUpdatePeriod; }
    //! Automatically connect to FSX?
    /*! \return auto connect? */
    bool isAutoConnectFsx() const { return _autoConnectFsx; }
    //! Automatically start web server?
    /*! \return auto start? */
    bool isAutoStartWebServer() const { return _autoStartWebServer; }
    //! Radius considered when fetching data
    /*! \param radiusMeter */
    void setFsxAircraftRadiusMeter(const int radiusMeter) { _fsxAircraftRadiusMeter = radiusMeter; }
    //! Radius considered when fetching data
    /*! \param radiusKilometer */
    void setFsxAircraftRadiusKilometer(const double radiusKilometer) { _fsxAircraftRadiusMeter = radiusKilometer * 1000; }
    //! FSX update period
    /*! \param updatePeriod */
    void setFsxUpdatePeriod(const SIMCONNECT_PERIOD updatePeriod) { _fsxUpdatePeriod = updatePeriod; }
    //! FSX IP address
    /*! \param ip */
    void setFsxIpAddress(const QString& ip) { _fsxIpAddress = HttpHelper::formatIPAddress(ip, false); }
    //! FSX IP port
    /*! \param port */
    void setFsxIpPort(const int port) { _fsxIpPort = port; }
    //! Max number of waypoints
    /*! \param maxWps */
    void setFsxModelMaxWaypoints(const int maxWps) { _fsxModelMaxWaypoints = maxWps; }
    //! Longest time frame for waypoints
    /*! \param timeframeS */
    void setFsxModelWaypointsTimeframeS(const int timeframeS) { _fsxModelWaypointsTimeframeS = timeframeS; }
    //! Use time frame restriction?
    /*! \param disabled */
    void setFsxModelWaypointsTimeframeDisabled(const bool disabled) { _fsxModelWaypointsTimeframeSDisabled = disabled; }
    //! Use max. waypoints restriction?
    /*! \param disabled */
    void setFsxModelMaxWaypointsDisabled(const bool disabled) { _fsxModelMaxWaypointsDisabled = disabled; }
    //! Dump statistics info
    /*! \param dump */
    void setFsxDumpStatistics(const bool dump) { _fsxDumpStatistics = dump; }
    //! Update period of aircrafts in milliseconds
    /*! \param milliseconds */
    void setGuiUpdateAircraftsMs(const int milliseconds) { _guiUpdateAircraftsMs = milliseconds; }
    //! Update period of aircrafts in seconds
    /*! \param seconds */
    void setGuiUpdateAircraftsS(const int seconds) { _guiUpdateAircraftsMs = seconds * 1000; }
    //! Directly show waypoints when an aircraft is selected
    /*! \param show */
    void setGuiShowWaypointsWhenAircraftSelected(const bool show) { _guiShowWaypointsWhenAircraftSelected = show;}
    //! Show only my (own) aircraft
    /*! \param onlyOwnAircraft */
    void setGuiShowOnlyOwnAircraft(const bool onlyOwnAircraft) { _guiShowOnlyOwnAircraft = onlyOwnAircraft; }
    //! Line style color
    /*! \param color */
    void setKmlLineStyleColor(const QString& color) { _kmlLineStyleColor = color; }
    //! Poly style color
    /*! \param color */
    void setKmlPolyStyleColor(const QString& color) { _kmlPolyStyleColor = color; }
    //! Set KML waypoint model
    /*! \param model */
    void setKmlWaypointModel(const QString& model) { _kmlWaypointModel = model; }
    //! Set KML line width
    /*! \param width */
    void setKmlLineStyleWidth(const int width) {_kmlLineStyleWidth = width; }
    //! Use the local model.
    /*! \param localModel */
    void setKmlWhenSavedUseLocalModel(const bool localModel) { _kmlWhenSavedUseLocalModel = localModel;}
    //! Auto start web server?
    /*! \param autoStart */
    void setAutoStartWebServer(const bool autoStart) { _autoStartWebServer = autoStart; }
    //! Automatically connect to FSX?
    /*! \param autoConnect */
    void setAutoConnectFsx(const bool autoConnect) { _autoConnectFsx = autoConnect; }
    //! Use the local model with a full path when saved?
    /*! \param fullPath */
    void setKmlWhenSavedUseFullPath(const bool fullPath) { _kmlWhenSavedUseFullPath = fullPath;}
    //! Period [s] for the network link
    /*! \param periodS */
    void setKmlNetworkLinkUpdatePeriodS(const int periodS) { _kmlNetworkLinkUpdatePeriodS = periodS; }
    //! FMC data directory
    /*! \param dir */
    void setFmcDataDirectory(const QString& dir) { _dirFmcData = dir; }
    //! Read from disk.
    /*! \param file and path */
    bool read(const QString &file = "");
    //! Write to disk
    bool write();
    //! Extract the SimConnect settings
    /*! \return FSXSimConnectSettings */
    FSXSimConnectSettings toFsxSimConnectSettings();
    //! Extract the KML settings
    /*! \return KmlSettings */
    KmlSettings toKmlSettings();
};
} // namespace
#endif // FSXWSSETTINGS_H
